package service;



import common.Message;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author 余一
 * @date 2021/10/30  12:04
 */
public class ThreadManager {
    private static HashMap<String,ServerClientThread> threadHashMap = new HashMap<>();

    /**
     * 添加线程
     * @param userId
     * @param thread
     */
    public static void addThread(String userId,ServerClientThread thread){
        threadHashMap.put(userId,thread);
    }

    /**
     * 获取userId对应的线程
     * @param userId
     * @return
     */
    public static ServerClientThread getThread(String userId){
       return threadHashMap.get(userId);
    }

    /**
     * 获取在线用户
     * @return
     */
    public static String getOnlineUser(){
        StringBuilder stringBuilder = new StringBuilder();
        threadHashMap.forEach((userId,serverThread) -> {
            stringBuilder.append(userId+"-");
        });
        return stringBuilder.toString();
    }

    /**
     * 删除一个在线用户
     * @param userId
     */
    public static void removeUser(String userId){
        threadHashMap.remove(userId);
    }

    /**
     * 获取所有用户的通信线程集合
     * @return
     */
    public static HashMap<String,ServerClientThread> getThreadHashMap(){
        return threadHashMap;
    }

    /**
     * 判断该用户是否离线
     * @param userId
     * @return
     */
    public static boolean ifOnline(String userId){
        if (threadHashMap.get(userId) == null) {
            return true;
        }
        return false;
    }
}
